package com.xyh.reward_kotlin.filter

import android.text.InputFilter
import android.text.Spanned

class InputFilterMinMax(private val min: Int, private val max: Int) : InputFilter {

    override fun filter(
        source: CharSequence,
        start: Int,
        end: Int,
        dest: Spanned,
        dstart: Int,
        dend: Int
    ): CharSequence? {
        val newVal = dest.substring(0, dstart) + source + dest.substring(dend)
        return try {
            val input = newVal.toInt()
            if (input in min..max) null else ""
        } catch (e: NumberFormatException) {
            ""
        }
    }
}
